import router from "@/management/router";
import { getAuthMenuListApi } from '@/management/api/layouts'

export default {
  setGlobalState({ commit }, data) {
    commit('setGlobalState', data)
  },
  addTabs({ commit, state }, tabItem) {
    if (state.tabsMenuList.every(item => item.path !== tabItem.path)) {
      commit('addTabs', tabItem)
    }
  },
  // Remove Tabs
  removeTabs({ commit, state }, { tabPath, isCurrent }) {
    if (isCurrent) {
      state.tabsMenuList.forEach((item, index) => {
        if (item.path !== tabPath) return;
        const nextTab = state.tabsMenuList[index + 1] || state.tabsMenuList[index - 1];
        if (!nextTab) return;
        router.push(nextTab.path);
      });
    }

    // set tabs
    let tabsMenuList = state.tabsMenuList.filter(item => item.path !== tabPath);
    commit('setTabsMenuList', tabsMenuList)
  },
  // Close Tabs On Side
  async closeTabsOnSide({ commit, state }, { path, type }) {
    const currentIndex = state.tabsMenuList.findIndex(item => item.path === path);
    if (currentIndex !== -1) {
      const range = type === "left" ? [0, currentIndex] : [currentIndex + 1, state.tabsMenuList.length];
      let tabsMenuList = state.tabsMenuList.filter((item, index) => {
        return index < range[0] || index >= range[1] || !item.close;
      });
      commit('setTabsMenuList', tabsMenuList)
    }
  },
  // Close MultipleTab
  async closeMultipleTab({ commit, state }, { tabsMenuValue }) {
    let tabsMenuList = state.tabsMenuList.filter(item => {
      return item.path === tabsMenuValue || !item.close;
    });
    commit('setTabsMenuList', tabsMenuList)
  },
  // Set Tabs
  async setTabs({ commit }, tabsMenuList) {
    commit('setTabsMenuList', tabsMenuList)
  },

  async getAuthMenuList({ commit }) {
    const res = await getAuthMenuListApi();
    commit('setAuthMenuList', res?.data);
  },

  async setRouteName({ commit }, data) {
    commit('setRouteName', data)
  },
}
